package com.baiye.template.controller;

import com.baiye.template.common.R;
import com.baiye.template.pojo.User;
import com.baiye.template.service.UserService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.messaging.MessagingException;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/8/26 16:09
 */
@RestController
@Api(value = "用户")
@RequestMapping("/user")
public class UserController {
    Logger log = LoggerFactory.getLogger(UserController.class);
    private final String avatar= "avatar.jpg";


    @Autowired
    private UserService userService;
    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String username;


    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public R register(@RequestBody User u){
        int i = userService.regiseter(u);
        if(i==1){
            return new R().ok("注册成功！");
        }else {
            return new R().error("注册失败，请重试");
        }
    }
//    @RequestMapping(value = "/login",method = RequestMethod.POST)
//    public R login(@RequestBody User u){
//        User user = userService.login(u);
//        if(user.getUsername()!=null){
//            return new R().ok(user);
//        }else {
//            return new R().error("账号密码错误");
//        }
//    }


    @RequestMapping(value = "/sendEmail",method = RequestMethod.GET)
    public R sendEmail(String email) throws MessagingException {
        SimpleMailMessage message = new SimpleMailMessage();
        int i= (int) (Math.random() * 1000000);
        message.setFrom(username);
        message.setSubject("验证码");
        message.setTo(email);
        message.setText("验证码为：  "+i+"   by baiye");
        javaMailSender.send(message);
        return new R().ok(i);
    }
    @RequestMapping(value = "/updateUserInfo",method = RequestMethod.POST)
    public R updateUserInfo(User user, @RequestParam(value = "file",required = false) MultipartFile file) throws IOException {
        if(file!=null){
            //String path1=ClassUtils.getDefaultClassLoader().getResource("static/avatar").getPath();
            //String path2=ResourceUtils.getURL("classpath:").getPath()+"static/avatar";
            //System.out.println(path1+"-------"+path2);
            String filename = user.getId()+avatar;
            File filePath = new File(new File("src/main/resources/static/avatar").getAbsolutePath(), filename);
            if (!filePath.getParentFile().exists()) {
                filePath.getParentFile().mkdirs();
                log.info("创建目录--{}",filePath);
            }
            file.transferTo(filePath);
            user.setImg("/"+avatar+"/"+filename);
        }
        userService.updateUserInfo(user);
        return new R().ok();
    }
}
